﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace BSCMFF.Web.UserControls.Questionaire
{
    public partial class QuestionaireCreation : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                CreateQuestions();
            }
            else
            {
                questionNumber = null;
            }
        }

        private int numberOfQuestions;

        protected void RadButton1_Click(object sender, EventArgs e)
        {
            //Session["questionNumber"] = (int)RadNumericTextBox1.Value;
            questionNumber = (int)RadNumericTextBox1.Value;
            CreateQuestions();
        }

        private static int? questionNumber;
        private void CreateQuestions()
        {
            if (questionNumber == null)
            {
                return;
            }
            else
            {
                numberOfQuestions = (int)questionNumber; // (int)RadNumericTextBox1.Value; //Convert.ToInt32(Session["questionNumber"]);
            }
            Table table = new Table();
            table.ID = "TableQuestions";



            TableRow headerRow = new TableRow();
            TableCell numberColumn = new TableCell();
            numberColumn.Text = "Number";

            TableCell questionColumn = new TableCell();
            questionColumn.Text = "Questions";


            TableCell selectAllColumn = new TableCell();
            selectAllColumn.Text = "Select All";

            CheckBox chSelectAll = new CheckBox();
            chSelectAll.ID = "CheckBoxSelectAll";
            //chSelectAll.CheckedChanged += new EventHandler(chSelectAll_CheckedChanged);
            chSelectAll.Attributes.Add("onclick", "SelectCheckBoxes();");
            selectAllColumn.Controls.Add(chSelectAll);

            headerRow.Controls.Add(numberColumn);
            headerRow.Controls.Add(questionColumn);
            headerRow.Controls.Add(selectAllColumn);

            table.Controls.Add(headerRow);


            for (int i = 0; i < numberOfQuestions; ++i)
            {
                Label number = new Label();
                number.Text = (i + 1).ToString() + ". ";

                RadTextBox rtb = new RadTextBox();
                rtb.Width = 500;
                rtb.ID = "RadTextBoxQuestion" + (i + 1).ToString();

                CheckBox chb = new CheckBox();
                chb.ID = "CheckBoxAnswers" + (i + 1).ToString();


                TableRow tr = new TableRow();
                TableCell td1 = new TableCell();
                td1.Controls.Add(number);
                TableCell td2 = new TableCell();
                td2.Controls.Add(rtb);
                TableCell td3 = new TableCell();
                td3.Controls.Add(chb);

                tr.Controls.Add(td1);
                tr.Controls.Add(td2);
                tr.Controls.Add(td3);

                table.Controls.Add(tr);

                TableRow trAnswers = new TableRow();
                TableCell tc = new TableCell();
                //tc.ColumnSpan = 3;
                //tc.Text = "tralala";
                trAnswers.Controls.Add(new TableCell());
                trAnswers.Controls.Add(tc);
                trAnswers.Controls.Add(new TableCell());

                table.Controls.Add(trAnswers);
            }

            PanelQuestions.Controls.Add(table);
        }

        void chSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            //Table t = (Table)PanelQuestions.FindControl("TableQuestions");

            //foreach (TableRow tr in t.Controls)
            //{
                
            //    ((CheckBox)tr.Controls[2].FindControl("CheckBoxAnswers")).Checked = true;
            //}
        }

        protected void ButtonAnswers_Click(object sender, EventArgs e)
        {
            //Table questions = PanelQuestions.FindControl("
            //foreach(TableRow tr in 
            //Label1.Text = ((CheckBox)((Table)PanelQuestions.Controls[1]).Controls[1].Controls[2].Controls[0]).Checked.ToString(); // ((CheckBox)Page.FindControl("CheckBoxAnswers1")).Checked.ToString();
            PanelAnswers.Visible = true;
        }

        private static List<int> createdAnswers = new List<int>();
        protected void ButtonApplyAnswers_Click(object sender, EventArgs e)
        {
            Table tQuestions = PanelQuestions.FindControl("TableQuestions") as Table;

            for (int i = 2; i < tQuestions.Rows.Count; i += 2)
            {
                TableRow rQuestion = tQuestions.Rows[i - 1];
                 
                TableCell checkbox = rQuestion.Cells[2];

                TableRow rAnswers = tQuestions.Rows[i];
                TableCell tcAnswers = rAnswers.Cells[1];
                tcAnswers.Controls.Clear();

                if (((CheckBox)checkbox.Controls[0]).Checked)
                {
                    int answerNumber = 1;
                    //tcAnswers.Controls.Clear();
                    //RadioButtonList rbl = new RadioButtonList();


                    foreach (Control c in PanelAnswers.Controls)
                    {
                        if (c.GetType() == typeof(RadTextBox) && ((RadTextBox)c).Text != "")
                        {
                            string answer = ((RadTextBox)c).Text;

                            RadioButton rb = new RadioButton();
                            rb.Text = answer;
                            rb.ID = "row_answer" + (i).ToString() + answerNumber.ToString();
                            rb.GroupName = "groupName" + (i).ToString();
                            ++answerNumber;


                            tcAnswers.Controls.Add(rb);                            
                        }
                    }
                    Session["tcAnswers" + i.ToString()] = tcAnswers;

                }
                else
                {
                    if (Session["tcAnswers" + i.ToString()] != null)
                    {
                        foreach (Control c in ((TableCell)Session["tcAnswers" + i.ToString()]).Controls)
                        {
                            tcAnswers.Controls.Add(c);
                        }
                    }
                }
            }
        }
    }
}